package List;

import java.util.Arrays;

public class MyArrayList implements iList{
    private static final int DEFAULT_SIZE = 10;
    int[] elem;
    int usedSize;//0

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public void add(int data) {
        //尾插
       if(isFull()){
           //扩容
            this.elem = Arrays.copyOf(elem,2 * elem.length);
        }
        this.elem[usedSize] = data;
       this.usedSize++;
    }

    @Override
    public boolean isFull() {

        return usedSize == elem.length;
    }

    @Override
    public void add(int pos, int data) {
        //指定位置插入
        try{
            checkPosOfAdd(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        for (int i = usedSize - 1; i >= pos; i--) {
            elem[i + 1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }
    private void checkPosOfAdd(int pos) throws PosNotLegalException{
        if(pos < 0 || pos >= usedSize) {
            // = usedSize时位置为空
            throw new PosNotLegalException("pos位置不合法！");
        }
    }

    @Override
    public boolean contains(int toFind) {
        //遍历找toFind
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        //返回找到toFind的第一个下标
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind){
                return i;
            }
        }
        return -1;//没找到
    }

    @Override
    public int get(int pos) {
        try{
            checkPosOfGetAndSet(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        return elem[pos];
    }
    private void checkPosOfGetAndSet(int pos) throws PosNotLegalException{
        if(pos < 0 || pos >= usedSize) {
            throw new PosNotLegalException("get/set获取元素的时候" +
                    "pos位置不合法！");
        }
    }

    @Override
    public void set(int pos, int value) {
        //更新目标地址的值
        try{
            checkPosOfGetAndSet(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        elem[pos] = value;
    }

    @Override
    public void remove(int key) {
        //查找key
        int pos = indexOf(key);
        if (pos == -1){
            System.out.println("没有要删除的数字");
            return;
        }
        //移动元素
        for (int i = pos; i < usedSize - 1; i++) {
            elem[i] = elem[i + 1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        usedSize = 0;
    }
}
